﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;
using System.Web.UI.WebControls.WebParts;

public partial class Task : System.Web.UI.Page
{
    public string ContainerPage = "./Tasking.aspx";
    public string strCondition = "";
    public string strError = "";
    public string strMess = "";
    public int nPermission = 0;
    public string oldComment = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["logined_id"] != null)
        {
            if (int.Parse(Session["tasking"].ToString()) > 0) nPermission = int.Parse(Session["tasking"].ToString());
            if (int.Parse(Session["superadmin"].ToString()) > 0) nPermission = 10;
            if (nPermission <= 0) Response.Redirect("Login.aspx");

        }
        else
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                loadObject();
            }
        }
    }
    protected void loadObject()
    {
        Tasking obj = new Tasking();
        obj.ID = long.Parse(Request.QueryString["ID"]);
        obj.Load(obj.ID, GV.cn, GV.trans);
        txtPriority.Text = obj.priority.ToString();
        //txtSort.Text = obj.Sort.ToString();
        txtName.Text = obj.Detail;
        txtComment.Text = obj.Comment;
        txtDateEstimate.Text = obj.Summary;
        txtSourceName.Text = obj.SourceName;
        drpCoding.SelectedValue = obj.IsCompleted.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        Tasking obj = new Tasking();
        obj.ID = -1;
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(obj.ID, GV.cn, GV.trans);
        }
        string MaxID = DBModule.ExecuteQueryForOneResult("SELECT Max(ID) as ID FROM tbl_tasking", GV.cn, GV.trans);
        try
        {
            if (obj.ID <= 0)
                obj.Sort = int.Parse(MaxID) + 1;
        }
        catch { obj.Sort = 1; }
        //try
        //{
        //    obj.Sort = int.Parse(txtSort.Text);
        //}
        //catch { obj.Sort = 0; }

        //obj.Name = txtName.Value;
        //obj.FileName1 = txtFileName1.Text;
        try
        {
            string serverFileName = Path.GetFileName(txtFileName1.PostedFile.FileName);
            string ext = Path.GetExtension(serverFileName);
            if (ext != null && ext != "")
            {
                if (ext == ".doc" || ext == ".xls" || ext == ".docx" || ext == ".xlsx" || ext == ".pdf")
                {
                    string strPath = MapPath(".") + "\\Tasking\\";
                    txtFileName1.PostedFile.SaveAs(strPath + serverFileName);
                    obj.FileName1 = serverFileName;
                    obj.CreatedDate = DateTime.Now;
                }
                else
                {
                    strError += "File not specified";
                    strMess += "Chỉ được upload fie wolds, excel va pdf!";
                }
            }
        }
        catch (Exception err)
        {
        }
        obj.Detail = txtName.Text;
        obj.Comment = txtComment.Text;
        if (obj.Detail != txtName.Text || obj.Comment != txtComment.Text)
            obj.CreatedDate = DateTime.Now;
        try
        {
            string serverFileName = Path.GetFileName(txtFileName2.PostedFile.FileName);
            string ext = Path.GetExtension(serverFileName);
            if (ext != null && ext != "")
            {
                if (ext == ".doc" || ext == ".xls" || ext == ".docx" || ext == ".xlsx" || ext == ".pdf")
                {
                    string strPath = MapPath(".") + "\\Tasking\\";
                    txtFileName2.PostedFile.SaveAs(strPath + serverFileName);
                    obj.FileName2 = serverFileName;
                    obj.LastUpdatedDate = DateTime.Now;
                }
                else
                {
                    strError += "File not specified";
                    strMess += "Chỉ được upload fie wolds, exel và pdf!";
                }
            }
        }
        catch (Exception err)
        {
        }
        obj.SourceName = txtSourceName.Text;
        obj.Summary = txtDateEstimate.Text;
        if (obj.SourceName != txtSourceName.Text || obj.Summary != txtDateEstimate.Text)
            obj.LastUpdatedDate = DateTime.Now;
        try
        {
            if (txtPriority.Text != null && txtPriority.Text != "") obj.priority = int.Parse(txtPriority.Text);
        }
        catch { obj.priority = 0; }
        obj.IsCompleted = int.Parse(drpCoding.Text);
        if (strMess == "")
        {
            obj.Save(GV.cn, GV.trans);
            if (obj.ID == -1)
            {
                DataSet ds = DBModule.ExecuteQuery("SELECT MAX(id) from tbl_tasking limit 0,1;", GV.cn, GV.trans);
                obj.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            if (Request.QueryString["page_number"] != null)
                Response.Redirect("Task.aspx?id=" + obj.ID + "&page_number=" + Request.QueryString["page_number"].ToString());
            else
                Response.Redirect("Task.aspx?id=" + obj.ID);
        }
        else
            ErrMessage.Text = strMess;
    }
}